home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / jaz_clib.arc / JZCHKDSK.DMO < prev    next >
Text File  |  1989-04-09  |  9KB  |  294 lines

  1. /*
  2. ┌────────────────────────────────────────────────────────────────────────────┐
  3. │ Title   : jzchkdsk                                 │
  4. │ This program does many low disk functions similar to chkdsk.             │
  5. │ It demonstrates how to perform low level disk functions from "C".          │
  6. │            Usage: JZCHKDSK <Options> <Path>                 │
  7. │ Compile Spec: msc /Ox /Ze /Zp /I c:\msc\jaz jzchkdsk.dmo;             │
  8. │ link jzchkdsk,,,jzc jzscreen                             │
  9. │                                         │
  10. │ (C) JazSoft Software by Jack A. Zucker (301) 794-5950              │
  11. └────────────────────────────────────────────────────────────────────────────┘
  12. */
  13.  
  14. #include <jzdirect.h>
  15. #include <jaz.h>
  16.  
  17. #define SIGINT 2        /* int 23h break code            */
  18.  
  19. char startdir[65];        /* starting directory for search    */
  20. char gpath[65];         /* global path spec            */
  21. int gcount = 0;         /* count of non-contiguous clusters */
  22. char *gfat;            /* global fat table */
  23. int gbigfat;            /* true if big fat table */
  24. int gnumfiles = 0;        /* number of files processed        */
  25. int gnumclust = 0;        /* total clusters processed        */
  26. int gbytes,gsectors;        /* bytes per sectors,sectors/cluster*/
  27. int NONCONTIG = 0;        /* non contiguous list only        */
  28. int CONTIG    = 0;        /* list only contiguous files        */
  29. int QUIET     = 0;        /* quit mode. Don't list anything   */
  30. int BADSECTORS= 0;        /* look for bad sectors         */
  31. int ERROR     = 0;        /* error flag. Exit program        */
  32. int breakkey();
  33. int gdrive;     ;        /* global drive */
  34.  
  35. main(argc,argv)
  36. int argc;
  37. char **argv;
  38. {
  39.  
  40.   unsigned int walloc,wsectsize,wclusters;
  41.   TFAT wfat;
  42.   int w;
  43.   char far *wid;
  44.   unsigned int getbadsect(),wbadsect;
  45.   char wbuf[512],*s;
  46.   char *woption;
  47.   TDISKBLK *wdisk = (TDISKBLK *) wbuf;
  48.  
  49.   getcwd(gpath,64);        /* get current path */
  50.  
  51.   strcpy(startdir,argv[2]);    /* copy filespec to work variable */
  52.  
  53.   if (strlen(startdir) > 1 && startdir[1] == ':') {
  54.     gdrive = toupper(startdir[0]) - 65;
  55.     memcpy(startdir,startdir+2,strlen(startdir)-1);
  56.     jzlogdrv(gdrive);
  57.   }
  58.   else
  59.     gdrive = jzgetdrv();
  60.  
  61.   if (strcmp(startdir,"\\") == 0) startdir[0] = 0;
  62.  
  63.   strupr(startdir);        /* convert to upper case */
  64.  
  65.   woption = argv[1];        /* get options         */
  66.  
  67.   if (*woption == '-' || *woption == '/')
  68.     for(s = woption+1 ; *s ; s ++)
  69.       switch(toupper(*s)) {
  70.     case 'N' : NONCONTIG = 1;       /* list only non contiguous files */
  71.            break;
  72.     case 'C' : CONTIG = 1;          /* list only contiguous files */
  73.            break;
  74.     case 'Q' : QUIET  = 1;          /* don't list anything        */
  75.            NONCONTIG = 1;
  76.            CONTIG    = 1;
  77.            break;
  78.     case 'B' : BADSECTORS = 1;      /* look for bad sectors */
  79.            break;
  80.     case '/' :
  81.     case '-' : break;
  82.     case '?' : ERROR = 1;           /* set error flag for help text */
  83.            break;
  84.     default  : printf("\nIllegal option %c",*s);
  85.            ERROR = 1;        /* set error flag */
  86.            break;
  87.       }
  88.  
  89.   printf("\nJZCHKDSK (C) JazSoft by Jack A. Zucker (301) 794-5950 | 75766,1336");
  90.  
  91.   if (argc == 1 || ERROR || ((NONCONTIG || CONTIG) && argc < 3)) {
  92.     printf("\nUsage: JZCHKDSK <Options> <Path>");
  93.     printf("\nOptions: -c   List contiguous files");
  94.     printf("\n         -n   List non - contiguous files only");
  95.     printf("\n         -q   Quiet mode. Don't list files at all");
  96.     printf("\n         -b   List Bad Sectors");
  97.     printf("\n");
  98.     exit(0);
  99.   }
  100.  
  101.   signal(SIGINT,breakkey);    /* handle control breaks elegantly */
  102.  
  103.   diskinfo(gdrive,wdisk);
  104.  
  105.   gbytes = wdisk->bytes;
  106.   gsectors = wdisk->sectors;
  107.  
  108.   jzdskfre(&wfat,gdrive+1);         /* get disk free space */
  109.   wid = jzfat(gdrive+1,&walloc,&wsectsize,&wclusters);
  110.  
  111.   if (*wid == 0xFF || *wid == 0xFE) {        /* 8 sector formats */
  112.     printf("\nCan't Read this disk");
  113.     breakkey();
  114.   }
  115.  
  116.   gbigfat = wclusters > 4086;
  117.  
  118.   if (CONTIG || NONCONTIG) {
  119.     printf("\n\n     File                               Total      Non       Dir       Actual");
  120.     printf("\n     Name                               Clusters   Contig    Size      Size\n");
  121.   }
  122.  
  123.   jzgetfat(&gfat,gdrive);      /* get the FAT for the spec drive */
  124.  
  125.   if (NONCONTIG || CONTIG) searchdir(startdir);  /* search the directory */
  126.  
  127.   if (BADSECTORS) wbadsect = getbadsect(gdrive,wdisk->ttlsect);
  128.  
  129.   if (NONCONTIG || CONTIG) {
  130.     printf("\n\nDrive %c",gdrive + 65);
  131.     printf("\nNon Contiguous Clusters             %d",gcount);
  132.     printf("\nNumber of files Processed           %d",gnumfiles);
  133.     printf("\nPercentage of Contigous files       %3.2f",
  134.          (float) (gnumclust - gcount) / gnumclust * 100);
  135.     printf("\nPercentage of Non Contiguous files  %3.2f",
  136.           (float) gcount / gnumclust * 100);
  137.   }
  138.  
  139.   printf("\nTotal Disk Space                    %ld",
  140.         (long) walloc * wsectsize * wclusters);
  141.   printf("\nTotal Clusters                      %u",wclusters);
  142.   printf("\nFree Space (Bytes)                  %ld",wfat.free);
  143.   printf("\nBad Sectors                         ");
  144.   printf(BADSECTORS ? "%u" : "<Option Not Selected>",wbadsect);
  145.  
  146.   chdir(gpath);     /* conveniently change back to default drive */
  147.   jzlogdrv(gpath[0]-65);     /* get back to original drive          */
  148.  
  149. }
  150.  
  151. searchdir(fpath)
  152. char *fpath;
  153.  
  154. #define SUBDIR 0x10        /* attribute for sub directory */
  155. #define NORMAL 0x20        /* attribute for normal direct */
  156. {
  157.   int werr;
  158.   TDIR wdir;            /* hold the dos error number   */
  159.   int w;            /* work variable           */
  160.   char path[65],temp[65];    /* work string variables       */
  161.   char dummy[65];
  162.  
  163.   if (*fpath) chdir(fpath);
  164.   else chdir("\\");
  165.  
  166.   strcpy(path,fpath);        /* copy new file spec to work string */
  167.   strcat(path,"\\*.*");         /* put search spec in                */
  168.  
  169.   /* get subdirs and normal files */
  170.  
  171.   werr = jzfndfst(path,NORMAL | SUBDIR,&wdir);
  172.  
  173.   if (! werr )            /* we found at least one match */
  174.     do {
  175.       if (wdir.attr == SUBDIR && wdir.name[0] != '.') {
  176.  
  177.     strcpy(path,fpath);    /* start from scratch */
  178.  
  179.     strcat(path,"\\");      /* create a path */
  180.  
  181.     strcat(path,wdir.name); /* append the new subdir       */
  182.  
  183.     searchdir(path);    /* recurse against the new file spec */
  184.  
  185.     if (*fpath) chdir(fpath);
  186.     else chdir("\\");
  187.       }
  188.  
  189.       if (wdir.attr != SUBDIR)    {
  190.     checkfile(wdir.name,fpath);
  191.     gnumfiles ++;
  192.       }
  193.  
  194.       werr = jzfndnxt(&wdir);           /* get the next directory item */
  195.  
  196.     } while (! werr);                  /* loop until no more files    */
  197.   else {
  198.     printf("\nError in Search Spec. Aborting...");
  199.     exit(0);
  200.   }
  201. }
  202.  
  203. checkfile(fname,fpath)
  204. char *fname;
  205. char *fpath;
  206. {
  207.   TFCB wfcb;
  208.   int w,wcluster,previous,wcount;
  209.   char wname[100];
  210.   int wclustercount;
  211.  
  212.   wcount = 1;            /* i.e. 0x200 , 0x202 = 2 non-contiguous */
  213.   wclustercount = 0;
  214.  
  215.   jzgetfcb(&wfcb,fname,0);
  216.  
  217.   wcluster = wfcb.cluster;
  218.   previous = wcluster - 1;    /* save previous cluster */
  219.  
  220.   do {
  221.  
  222.     gnumclust ++;            /* increment cluster count */
  223.     wclustercount ++;            /* increment cluster count */
  224.     if (NONCONTIG)
  225.       if (wcluster != (previous + 1)) {
  226.     wcount ++;          /* increment local count */
  227.     gcount ++;          /* increment global count */
  228.     previous = wcluster;
  229.       }
  230.       else
  231.     previous ++;
  232.  
  233.     wcluster = jzgetcls(gfat , wcluster , gbigfat);
  234.  
  235.   } while (! jzfateof(wcluster , gbigfat));
  236.  
  237.   if ((wcount > 1 && NONCONTIG) || (wcount == 1 && CONTIG)) {
  238.     if ( ! QUIET ) {
  239.       strcpy(wname,fpath);
  240.       strcat(wname,"\\");
  241.       strcat(wname,fname);
  242.  
  243.       printf("\n%-36s     %4d     %4d     %7ld    %7ld",wname,
  244.          wclustercount , wcount == 1 ? 0 : wcount,
  245.          wfcb.size , (long) wclustercount * gsectors * gbytes);
  246.     }
  247.  
  248.     if (wcount > 1) gcount ++;      /* account for 1st cluster */
  249.   }
  250.  
  251. }
  252.  
  253. breakkey()
  254. {
  255.   chdir(gpath);         /* get back to original path */
  256.   jzlogdrv(gpath[0]-65);    /* get back to logged drive  */
  257.   exit(0);
  258. }
  259.  
  260. unsigned int
  261. getbadsect( fdisk , fttlsect)
  262. int fdisk;
  263. unsigned int fttlsect;
  264. {
  265.   #define BLKSIZE 32
  266.   unsigned int wpartitions,wleftover,w,w2,wcount = 0;
  267.   int werr;
  268.   char *wbuf,*malloc();
  269.  
  270.   printf("\nChecking for bad sectors. Please be patient. . .\n");
  271.  
  272.   if (!(wbuf = malloc(512 * BLKSIZE))) {    /* 32 sector blocks     */
  273.     printf("\nNot enough memory for sector blocks. Aborting...");
  274.     exit(-1);
  275.   }
  276.  
  277.   wpartitions = fttlsect / BLKSIZE;       /* find total 32 sector partitions */
  278.  
  279.   wleftover   = fttlsect % BLKSIZE;       /* find remaining sectors          */
  280.  
  281.   for (w = 0 ; w < wpartitions ; w ++ )     /* search through partition */
  282.     if (dosreads(fdisk,w*BLKSIZE,BLKSIZE,wbuf)) /* if error in partition    */
  283.       for (w2 = 0 ; w2 < BLKSIZE ; w2 ++)    /* search by sector        */
  284.     if (dosreads(fdisk,w2+w*BLKSIZE,1,wbuf)) {
  285.       printf("%0004X    ",w2+w*BLKSIZE);
  286.       wcount ++;
  287.     }
  288.  
  289.   if (dosreads(fdisk,wpartitions*BLKSIZE,wleftover,wbuf)) /* search leftovers */
  290.     printf("%0004X    ",w*BLKSIZE);
  291.  
  292.   return(wcount);
  293. }
  294.